import 'package:flutter/material.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';

class CartCounter extends StatefulWidget {
  final ValueChanged<int> onChange;

  const CartCounter({super.key, required this.onChange});

  @override
  _CartCounterState createState() => _CartCounterState();
}

class _CartCounterState extends State<CartCounter> {
  int _quantity = 1;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: MarginPaddingUtil.all(10),
      decoration: BoxDecoration(
        color: Color(0xFFF6F6F6),
        borderRadius: BorderRadius.circular(22.sp),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          GestureDetector(
            onTap: () {
              setState(() {
                if (_quantity > 1) {
                  _quantity--;
                  widget.onChange.call(_quantity);
                }
              });
            },
            child: ImageAsset(
              "/icon/ic_minus",
              size: 30.width,
            ),
          ),
          37.widthBox,
          TextCommon(
            '$_quantity',
            size: 24.sp,
            color: Color(0xFF707070),
          ),
          37.widthBox,
          GestureDetector(
            onTap: () {
              setState(() {
                _quantity++;
                widget.onChange.call(_quantity);
              });
            },
            child: ImageAsset(
              "/icon/ic_add",
              size: 30.width,
            ),
          ),
        ],
      ),
    );
  }
}
